<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{obj.title}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="obj.aut_name" :label="obj.pubdate|Datefly">
        <template #icon>
          <img :src="obj.aut_photo" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button type="info" size="mini" v-if="obj.is_followed" @click="followFn(true)">已关注</van-button>
            <van-button icon="plus" type="info" v-else size="mini" plain @click="followFn(false)"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="obj.content"></div>

      <!-- 分割线 -->
      <div id="gettop" style="height:24px">
      </div>
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box" >
        <van-button @click="fn(1)" icon="good-job" v-if="obj.attitude===1" type="danger" size="small"
          >已点赞</van-button
        >
        <van-button @click="fn(-1)" icon="good-job-o" v-else type="danger" plain size="small"
          >点赞</van-button
        >
      </div>
    </div>
    <Comment v-if="obj.art_id" :artId='obj'/>
  </div>
</template>

<script>
import Comment from './Comment.vue'
import { getArticleDetailAPI, followAutAPI, unfollowAutAPI, articleLikeAPI, articleDisLikeAPI } from '@/api/article.js'
import { Toast } from 'vant'
export default {
  components: {
    Comment
  },
  data () {
    return {
      obj: {}
    }
  },
  async created () {
    const res = await getArticleDetailAPI({
      Id: this.$route.query.art_id
    })
    this.obj = res.data
  },
  methods: {
    async followFn (Boolean) {
      this.obj.is_followed = !this.obj.is_followed
      try {
        Boolean ? await unfollowAutAPI(this.obj.aut_id) : await followAutAPI(this.obj.aut_id)
        Toast.success(Boolean ? '取关成功' : '关注成功')
      } catch (error) {
        Toast.fail(Boolean ? '取关成功' : '关注成功')
      }
    },
    async fn (id) {
      this.obj.attitude = this.obj.attitude === 1 ? -1 : 1
      id === 1 ? await articleDisLikeAPI(this.obj.art_id) : await articleLikeAPI(this.obj.art_id)
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
